package com.example.minefieldkeepercloud.activity.machineslist;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.adapter.MDapter;
import com.example.minefieldkeepercloud.adapter.Madapter;
import com.example.minefieldkeepercloud.bean.CqBean;
import com.example.minefieldkeepercloud.bean.CqqBean;
import com.example.minefieldkeepercloud.bean.FactoryNewBean;
import com.example.minefieldkeepercloud.bean.MacListBean;
import com.example.minefieldkeepercloud.bean.TmdBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.network.HttpUtils;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.TimeCountUtil;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.view.dialog.DelayeDialog;
import com.example.minefieldkeepercloud.view.dialog.LoadingDialog;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 矿机详情
 */
public class MachineDetailAc extends AppCompatActivity {
    private MDapter mDapter;
    private List<TmdBean> listData;
    @BindView(R.id.recyleview)
    ListView recyleview;
    @BindView(R.id.listview)
    ListView listview;
    private List<String> list=new ArrayList<>();
    private Madapter madapter;
    private MacListBean.DataBean bean;
    private String factoryID;
    private List<String> list_number=new ArrayList<>();
    private String ControlOrder="";//2 刷新 1重启
    private String DataId="";
    private DelayeDialog loadingDialog;
    private Handler handler = new Handler();
    private boolean b=true;//倒计时是否完成 完成择访问失败
    private String BZID="";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_md);
        ButterKnife.bind(this);
        InitView();
        getData();
    }

    private void InitView() {
        bean = (MacListBean.DataBean) getIntent().getBundleExtra("bd").getSerializable("data");
        factoryID=getIntent().getBundleExtra("bd").getString("factoryID");
        BZID=getIntent().getBundleExtra("bd").getString("BZID");
        listData = new ArrayList<>();
        mDapter = new MDapter(this, listData);
        recyleview.setAdapter(mDapter);
        madapter= new Madapter(this, list,list_number);
        listview.setAdapter(madapter);

    }

    private void getData() {
        if (bean == null) {
            return;
        }

        TmdBean tmdBean=new TmdBean();
        tmdBean.setTitle("矿机IP");
        tmdBean.setKind(0);
        tmdBean.setContent(bean.getIP());
        listData.add(tmdBean);
        TmdBean tmdBean25=new TmdBean();
        tmdBean25.setTitle("矿机状态");
        tmdBean25.setKind(0);
        tmdBean25.setContent(bean.getMacRunStatus());
        listData.add(tmdBean25);

        TmdBean tmdBean1=new TmdBean();
        tmdBean1.setTitle("扫描时间");
        tmdBean1.setKind(0);

        try {
            tmdBean1.setContent(dealDateFormatNomm(bean.getUpdateTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        listData.add(tmdBean1);

        TmdBean tmdBean2=new TmdBean();
        tmdBean2.setTitle("客户名称");
        tmdBean2.setKind(0);
        tmdBean2.setContent(bean.getUserName());
        listData.add(tmdBean2);

        TmdBean tmdBean3=new TmdBean();
        tmdBean3.setTitle("矿机品牌");
        tmdBean3.setKind(0);
        tmdBean3.setContent(bean.getMacBrandName());
        listData.add(tmdBean3);

        TmdBean tmdBean4=new TmdBean();
        tmdBean4.setTitle("矿机型号");
        tmdBean4.setKind(0);
        tmdBean4.setContent(bean.getMacModelName());
        listData.add(tmdBean4);

        TmdBean tmdBean5=new TmdBean();
        tmdBean5.setTitle("实际功率");
        tmdBean5.setKind(0);
        tmdBean5.setContent(bean.getActualPower());
        listData.add(tmdBean5);

//        TmdBean tmdBean6=new TmdBean();
//        tmdBean6.setTitle("机位");
//        tmdBean6.setKind(0);
//        tmdBean6.setContent(bean.getPlaceName());
//        listData.add(tmdBean6);
//
        TmdBean tmdBean7=new TmdBean();
        tmdBean7.setTitle("实时算力");
        tmdBean7.setKind(0);
        tmdBean7.setContent(bean.getRelsl());
        listData.add(tmdBean7);

        TmdBean tmdBean8=new TmdBean();
        tmdBean8.setTitle("平均算力");
        tmdBean8.setKind(0);
        tmdBean8.setContent(bean.getAvgsl());
        listData.add(tmdBean8);

        TmdBean tmdBean9=new TmdBean();
        tmdBean9.setTitle("矿机温度");
        tmdBean9.setKind(0);
        tmdBean9.setContent(bean.getTemperature());
        listData.add(tmdBean9);

        TmdBean tmdBean10=new TmdBean();
        tmdBean10.setTitle("风扇转速");
        tmdBean10.setKind(0);
        tmdBean10.setContent(bean.getFanSpeed());
        listData.add(tmdBean10);

        TmdBean tmdBean11=new TmdBean();
        tmdBean11.setTitle("运行时间");
        tmdBean11.setKind(0);
        tmdBean11.setContent(bean.getMonRunTime());
        listData.add(tmdBean11);

        TmdBean tmdBean12=new TmdBean();
        tmdBean12.setTitle("固件版本");
        tmdBean12.setKind(0);
        tmdBean12.setContent(bean.getFirmwareVersion());
        listData.add(tmdBean12);

        TmdBean tmdBean13=new TmdBean();
        tmdBean13.setTitle("软件版本");
        tmdBean13.setKind(0);
        tmdBean13.setContent(bean.getSoftwareVersion());
        listData.add(tmdBean13);

        TmdBean tmdBean14=new TmdBean();
        tmdBean14.setTitle("硬件版本");
        tmdBean14.setKind(0);
        tmdBean14.setContent(bean.getHardwareVersion());
        listData.add(tmdBean14);

        TmdBean tmdBean15=new TmdBean();
        tmdBean15.setTitle("矿机频率");
        tmdBean15.setKind(0);
        tmdBean15.setContent(bean.getMineFrequency());
        listData.add(tmdBean15);

        TmdBean tmdBean16=new TmdBean();
        tmdBean16.setTitle("芯片数");
        tmdBean16.setKind(0);
        tmdBean16.setContent(bean.getChipsNumber());
        listData.add(tmdBean16);

        TmdBean tmdBean17=new TmdBean();
        tmdBean17.setTitle("算力板算力");
        tmdBean17.setKind(0);
        tmdBean17.setContent(bean.getForcePlatePower());
        listData.add(tmdBean17);

        TmdBean tmdBean18=new TmdBean();
        tmdBean18.setTitle("矿机名1");
        tmdBean18.setKind(0);
        tmdBean18.setContent(bean.getMachineName1());
        listData.add(tmdBean18);

        TmdBean tmdBean19=new TmdBean();
        tmdBean19.setTitle("矿池1");
        tmdBean19.setKind(0);
        tmdBean19.setContent(bean.getBitcoinPool1());
        listData.add(tmdBean19);

        TmdBean tmdBean20=new TmdBean();
        tmdBean20.setTitle("矿机名2");
        tmdBean20.setKind(0);
        tmdBean20.setContent(bean.getMachineName2());
        listData.add(tmdBean20);

        TmdBean tmdBean21=new TmdBean();
        tmdBean21.setTitle("矿池2");
        tmdBean21.setKind(0);
        tmdBean21.setContent(bean.getBitcoinPool2());
        listData.add(tmdBean21);

        TmdBean tmdBean22=new TmdBean();
        tmdBean22.setTitle("矿机名3");
        tmdBean22.setKind(0);
        tmdBean20.setContent(bean.getMachineName3());
        listData.add(tmdBean22);

        TmdBean tmdBean23=new TmdBean();
        tmdBean23.setTitle("矿池3");
        tmdBean23.setKind(0);
        tmdBean21.setContent(bean.getBitcoinPool3());
        listData.add(tmdBean23);

        TmdBean tmdBean24=new TmdBean();
        tmdBean24.setTitle("Mac地址");
        tmdBean24.setKind(0);
        tmdBean22.setContent(bean.getMacAddres());
        listData.add(tmdBean24);

        mDapter.setListData(listData);
        mDapter.notifyDataSetChanged();



        for (int i=0;i<bean.getMacStateZ().size();i++)
        {
            if(!bean.getMacStateZ().get(i).equals(""))
            {
                list_number.add(i+"");
                list.add(bean.getMacStateZ().get(i));
            }

        }


        madapter.setListData(list);
        madapter.notifyDataSetChanged();
        Log.d("xxxx",list.size()+"");
    }

    @OnClick({R.id.title_left,R.id.tv_wh,R.id.tv_wx,R.id.tv_sx,R.id.tv_cq})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.tv_wh:
                send("4");
                break;
            case R.id.tv_wx:
                send("2");
                break;
            case R.id.tv_sx://刷新
                if(loadingDialog!=null)
                    loadingDialog=null;


                loadingDialog=new DelayeDialog(MachineDetailAc.this,"数据刷新中......", new TimeCountUtil.FonfinishListener() {
                    @Override
                    public void onfinish() {
                        b=false;
                    }
                });
                ControlOrder="2";
                DataId="";
                b=true;
                refresh();
                break;
            case R.id.tv_cq://重启
                if(loadingDialog!=null)
                    loadingDialog=null;


                 loadingDialog=new DelayeDialog(MachineDetailAc.this,"矿机重启中......", new TimeCountUtil.FonfinishListener() {
                    @Override
                    public void onfinish() {
                        b=false;
                    }
                });
                ControlOrder="1";
                DataId="";
                b=true;
                refresh();
                break;
        }
    }

    private void send(String kind)
    {
        Intent intent=new Intent(MachineDetailAc.this, MchineStateAc.class);
        Bundle bd=new Bundle();
        bd.putString("factoryID",factoryID);
        bd.putString("kind",kind);
        bd.putString("MachineName1",bean.getMachineName1());
        bd.putString("IP",bean.getIP());
        intent.putExtra("bd",bd);
        startActivity(intent);
        overridePendingTransition(0, 0);

    }
    private void refresh()//刷新与重启
    {
        NetHelperNew.GetCQ_SX_Insert(factoryID,bean.getIP(),ControlOrder,new ApiHttpCallback(MachineDetailAc.this, 1,true) {
            @Override
            public void onSuccess(String data) {
                CqqBean cqBean = MyGson.getInstance().fromJson(data, CqqBean.class);
                if (cqBean.getType() == 1) {
                    DataId=cqBean.getData();
                    loadingDialog.show();
                    NextGo();
                } else {
                    ToastUtil.showToast(MachineDetailAc.this, cqBean.getContent());
                }
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                ToastUtil.showToast(MachineDetailAc.this, "请求失败");

            }
        });
    }

    /**
     * 持续访问
     * @param
     */
    private void NextGo()
    {
        NetHelperNew.GetCQ_SX_Loop(DataId,bean.getID(),BZID,new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                CqBean cqBean = MyGson.getInstance().fromJson(data, CqBean.class);
                if (cqBean.getType() == 1) {

                    if(cqBean.getCounts().equals("0"))
                    {
                        if(b)
                        {
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    NextGo();
                                }
                            }, 5000);

                        }


                    }
                    else //请求成功
                    {

                        loadingDialog.dismiss();
                        loadingDialog=null;
                        ToastUtil.showToast(MachineDetailAc.this, cqBean.getContent());
                        if(ControlOrder.equals("2"))//刷新数据
                        {
                            if(cqBean.getData().size()==0)
                            {
                                return;
                            }
                            listData.clear();
                            list_number.clear();
                            list.clear();

                            TmdBean tmdBean=new TmdBean();
                            tmdBean.setTitle("矿机IP");
                            tmdBean.setKind(0);
                            tmdBean.setContent(cqBean.getData().get(0).getIP());
                            listData.add(tmdBean);
                            TmdBean tmdBean25=new TmdBean();
                            tmdBean25.setTitle("矿机状态");
                            tmdBean25.setKind(0);
                            tmdBean25.setContent(cqBean.getData().get(0).getMacRunStatus());
                            listData.add(tmdBean25);

                            TmdBean tmdBean1=new TmdBean();
                            tmdBean1.setTitle("扫描时间");
                            tmdBean1.setKind(0);

                            try {
                                tmdBean1.setContent(dealDateFormatNomm(cqBean.getData().get(0).getUpdateTime()));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            listData.add(tmdBean1);

                            TmdBean tmdBean2=new TmdBean();
                            tmdBean2.setTitle("客户名称");
                            tmdBean2.setKind(0);
                            tmdBean2.setContent(cqBean.getData().get(0).getUserName());
                            listData.add(tmdBean2);

                            TmdBean tmdBean3=new TmdBean();
                            tmdBean3.setTitle("矿机品牌");
                            tmdBean3.setKind(0);
                            tmdBean3.setContent(cqBean.getData().get(0).getMacBrandName());
                            listData.add(tmdBean3);

                            TmdBean tmdBean4=new TmdBean();
                            tmdBean4.setTitle("矿机型号");
                            tmdBean4.setKind(0);
                            tmdBean4.setContent(cqBean.getData().get(0).getMacModelName());
                            listData.add(tmdBean4);

                            TmdBean tmdBean5=new TmdBean();
                            tmdBean5.setTitle("实际功率");
                            tmdBean5.setKind(0);
                            tmdBean5.setContent(cqBean.getData().get(0).getActualPower());
                            listData.add(tmdBean5);

//        TmdBean tmdBean6=new TmdBean();
//        tmdBean6.setTitle("机位");
//        tmdBean6.setKind(0);
//        tmdBean6.setContent(bean.getPlaceName());
//        listData.add(tmdBean6);
//
                            TmdBean tmdBean7=new TmdBean();
                            tmdBean7.setTitle("实时算力");
                            tmdBean7.setKind(0);
                            tmdBean7.setContent(cqBean.getData().get(0).getRelsl());
                            listData.add(tmdBean7);

                            TmdBean tmdBean8=new TmdBean();
                            tmdBean8.setTitle("平均算力");
                            tmdBean8.setKind(0);
                            tmdBean8.setContent(cqBean.getData().get(0).getAvgsl());
                            listData.add(tmdBean8);

                            TmdBean tmdBean9=new TmdBean();
                            tmdBean9.setTitle("矿机温度");
                            tmdBean9.setKind(0);
                            tmdBean9.setContent(cqBean.getData().get(0).getTemperature());
                            listData.add(tmdBean9);

                            TmdBean tmdBean10=new TmdBean();
                            tmdBean10.setTitle("风扇转速");
                            tmdBean10.setKind(0);
                            tmdBean10.setContent(cqBean.getData().get(0).getFanSpeed());
                            listData.add(tmdBean10);

                            TmdBean tmdBean11=new TmdBean();
                            tmdBean11.setTitle("运行时间");
                            tmdBean11.setKind(0);
                            tmdBean11.setContent(cqBean.getData().get(0).getMonRunTime());
                            listData.add(tmdBean11);

                            TmdBean tmdBean12=new TmdBean();
                            tmdBean12.setTitle("固件版本");
                            tmdBean12.setKind(0);
                            tmdBean12.setContent(cqBean.getData().get(0).getFirmwareVersion());
                            listData.add(tmdBean12);

                            TmdBean tmdBean13=new TmdBean();
                            tmdBean13.setTitle("软件版本");
                            tmdBean13.setKind(0);
                            tmdBean13.setContent(cqBean.getData().get(0).getSoftwareVersion());
                            listData.add(tmdBean13);

                            TmdBean tmdBean14=new TmdBean();
                            tmdBean14.setTitle("硬件版本");
                            tmdBean14.setKind(0);
                            tmdBean14.setContent(cqBean.getData().get(0).getHardwareVersion());
                            listData.add(tmdBean14);

                            TmdBean tmdBean15=new TmdBean();
                            tmdBean15.setTitle("矿机频率");
                            tmdBean15.setKind(0);
                            tmdBean15.setContent(cqBean.getData().get(0).getMineFrequency());
                            listData.add(tmdBean15);

                            TmdBean tmdBean16=new TmdBean();
                            tmdBean16.setTitle("芯片数");
                            tmdBean16.setKind(0);
                            tmdBean16.setContent(cqBean.getData().get(0).getChipsNumber());
                            listData.add(tmdBean16);

                            TmdBean tmdBean17=new TmdBean();
                            tmdBean17.setTitle("算力板算力");
                            tmdBean17.setKind(0);
                            tmdBean17.setContent(cqBean.getData().get(0).getForcePlatePower());
                            listData.add(tmdBean17);

                            TmdBean tmdBean18=new TmdBean();
                            tmdBean18.setTitle("矿机名1");
                            tmdBean18.setKind(0);
                            tmdBean18.setContent(cqBean.getData().get(0).getMachineName1());
                            listData.add(tmdBean18);

                            TmdBean tmdBean19=new TmdBean();
                            tmdBean19.setTitle("矿池1");
                            tmdBean19.setKind(0);
                            tmdBean19.setContent(cqBean.getData().get(0).getBitcoinPool1());
                            listData.add(tmdBean19);

                            TmdBean tmdBean20=new TmdBean();
                            tmdBean20.setTitle("矿机名2");
                            tmdBean20.setKind(0);
                            tmdBean20.setContent(cqBean.getData().get(0).getMachineName2());
                            listData.add(tmdBean20);

                            TmdBean tmdBean21=new TmdBean();
                            tmdBean21.setTitle("矿池2");
                            tmdBean21.setKind(0);
                            tmdBean21.setContent(cqBean.getData().get(0).getBitcoinPool2());
                            listData.add(tmdBean21);

                            TmdBean tmdBean22=new TmdBean();
                            tmdBean22.setTitle("矿机名3");
                            tmdBean22.setKind(0);
                            tmdBean20.setContent(cqBean.getData().get(0).getMachineName3());
                            listData.add(tmdBean22);

                            TmdBean tmdBean23=new TmdBean();
                            tmdBean23.setTitle("矿池3");
                            tmdBean23.setKind(0);
                            tmdBean21.setContent(cqBean.getData().get(0).getBitcoinPool3());
                            listData.add(tmdBean23);

                            TmdBean tmdBean24=new TmdBean();
                            tmdBean24.setTitle("Mac地址");
                            tmdBean24.setKind(0);
                            tmdBean22.setContent(cqBean.getData().get(0).getMacAddres());
                            listData.add(tmdBean24);

                            mDapter.setListData(listData);
                            mDapter.notifyDataSetChanged();



                            for (int i=0;i<cqBean.getData().get(0).getMacStateZ().size();i++)
                            {
                                if(!cqBean.getData().get(0).getMacStateZ().get(i).equals(""))
                                {
                                    list_number.add(i+"");
                                    list.add(cqBean.getData().get(0).getMacStateZ().get(i));
                                }

                            }

                            madapter.setListData(list);
                            madapter.notifyDataSetChanged();
                        }
                    }


                } else {
                    loadingDialog.dismiss();
                    loadingDialog=null;
                    ToastUtil.showToast(MachineDetailAc.this, cqBean.getContent());
                }
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                ToastUtil.showToast(MachineDetailAc.this, "请求失败");
                loadingDialog.dismiss();
                loadingDialog=null;

            }
        });

    }

    public  String dealDateFormatNomm(String oldDateStr) throws ParseException {
        if (!"".equals(oldDateStr))
        {
            String t = oldDateStr.replace("T", " ");
           String substring = t.substring(0, t.lastIndexOf("."));
            return substring;

        }
        return "";
    }
}
